/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.mp.common.api.handler;

import com.yami.shop.common.util.Json;
import com.yami.shop.mp.common.model.WeixinSubscribe;
import com.yami.shop.mp.common.service.WeixinSubscribeService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpKefuService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class SubscribeHandler extends AbstractHandler {


    private final WeixinSubscribeService weixinSubscribeService;

    @Autowired
    public SubscribeHandler(WeixinSubscribeService weixinSubscribeService) {
        this.weixinSubscribeService = weixinSubscribeService;
    }


    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) throws WxErrorException {
        this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
        // 关注时推送的消息
        String openid = wxMessage.getFromUser();
        List<WeixinSubscribe> weixinSubscribes = weixinSubscribeService.listWeixinSubscribeAndMaterial();
        WxMpKefuService kefuService = weixinService.getKefuService();
        for (WeixinSubscribe weixinSubscribe : weixinSubscribes) {
            String msgType = weixinSubscribe.getMsgType();
            WxMpKefuMessage kefuMessage = new WxMpKefuMessage();
            kefuMessage.setToUser(openid);
            kefuMessage.setMsgType(msgType);
            if (WxConsts.KefuMsgType.TEXT.equals(msgType)) {
                kefuMessage.setContent(weixinSubscribe.getContent());
            } else if (WxConsts.KefuMsgType.NEWS.equals(msgType)) {

                WxMpNewsArticle article = Json.parseObject(weixinSubscribe.getContent(), WxMpNewsArticle.class);

                WxMpKefuMessage.WxArticle wxArticle = new WxMpKefuMessage.WxArticle();
                wxArticle.setDescription(article.getDigest());
                wxArticle.setPicUrl(article.getThumbUrl());
                wxArticle.setTitle(article.getTitle());
                wxArticle.setUrl(article.getUrl());
                kefuMessage.setArticles(Collections.singletonList(wxArticle));

            } else {
                kefuMessage.setMediaId(weixinSubscribe.getMediaId());
            }
            kefuService.sendKefuMessage(kefuMessage);
        }
        return null;
    }

}
